package lobystate
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.controls.Alert;
	import poker.LevelDefine;
	
	public class StateUpdateRoomPlayerlist extends NetRequestState
	{
		private static var instance:StateUpdateRoomPlayerlist = null;
		
		public function StateUpdateRoomPlayerlist()
		{
			super();
		}
		
		public static function get Instance():StateUpdateRoomPlayerlist
		{
			if(instance == null)
				instance = new StateUpdateRoomPlayerlist();
			return instance;
		}
		
		override public function send(obj:StateManager):void
		{
			LobyNetManager.Instance.lobbyUpdater.url =LobyNetManager.URL_lobysonAddress;
			LobyNetManager.Instance.lobbyUpdater.request = {"PROCESS":LobyNetManager.URL_roomPlayerlist};
			LobyNetManager.Instance.lobbyUpdater.send();
		}
		override public function receive(obj:Object):Boolean
		{
			if(super.receive(obj))
			{
//				Alert.show("更新玩家列表");
				// 获得与大厅房间列表绑定的数据表
				var roomlist:ArrayCollection = StateGetRoomPlayerlist.Instance.roomlist;				
				roomlist.removeAll();
				for each(var leftplayer:Object in obj.players)
				{
					if(leftplayer.nickname != "")
					{
						var playerdata:Object = new Object();
						playerdata.userId = leftplayer.userId;
						playerdata.nickname = leftplayer.nickname;
						playerdata.score=leftplayer.score;						
						playerdata.money = leftplayer.money;
						playerdata.level = LevelDefine.getLevelName(playerdata.score);
						roomlist.addItem(playerdata);
					}
				}
				// 更新大厅的玩家数据列表
				FlexGlobals.topLevelApplication.roomplayerlist.invalidateList();				
				// 继续进入游戏的请求， 请求一次当时离开的房间的信息
				//				LobyNetManager.Instance.update(LobyNetManager.updateInvitelist);
				return true;
			}
			else{
				return false;
			}			
		}
		override public function fault(event:Event):void
		{
			
		}
	}
}